package com.demo.controller;

import com.demo.common.AppConfig;
import com.demo.common.AppResult;
import com.demo.common.ResultCode;
import com.demo.exception.ApplicationException;
import com.demo.model.Article;
import com.demo.model.ArticleReply;
import com.demo.model.User;
import com.demo.services.IArticleReplyService;
import com.demo.services.IArticleService;
import com.demo.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author quan
 * @Description 回复帖
 * @Date 2023/8/15 9:36
 */
@Api(tags = "回复帖子")
@Slf4j
@RestController
@RequestMapping("reply")
public class ArticleReplyController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IArticleReplyService articleReplyService;

    @ApiOperation("获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId (@ApiParam("帖子Id") @RequestParam("articleId") @NonNull Long articleId) {
        // 根据帖子Id查询出帖子详情，校验帖子是否有效
        Article article = articleService.selectById(articleId);
        // 帖子是否存在
        if (article == null || article.getDeleteState() == 1) {
            // 返回错误提示
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 帖子是否有效(封帖)
        if (article.getState() == 1) {
            // 返回错误提示
            return AppResult.failed("帖子已封帖");
        }

        // 调用Service，查询回复列表
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        if (articleReplies == null) {
            articleReplies = new ArrayList<>();
        }
        // 返回结果
        return AppResult.success(articleReplies);
    }


    @ApiOperation("发布回复帖")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                       @ApiParam("要回复的帖子Id") @RequestParam("articleId") @NonNull Long articleId,
                       @ApiParam("回复帖内容") @RequestParam("content") @NonNull String content){
        //1-校验用户的登录状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION_INFO);
        if(user.getState() == 1){
            // 表示已被禁言
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //2-获取要回复的帖子对象
        Article article = articleService.selectById(articleId);
        //2-1 判断要回复的帖子是否存在
        if(article == null || article.getDeleteState()==1){
            // 表示已删除或不存在
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //2-2 判断帖子是否封贴
        if(article.getState() == 1){
           //表示已封贴
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //3-构造回复帖对象，设置必传参数
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId); //要回复的帖子
        articleReply.setContent(content); //内容
        articleReply.setPostUserId(user.getId()); //回复帖作者
        //4-调用service在数据库写入回复帖
        articleReplyService.create(articleReply);
        //5-调用更新帖子 回复数 的service
        articleService.updateById(articleReply.getArticleId());
        //6-返回结果
        return AppResult.success("回复成功！");
    }
}
